// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "post_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "房屋名称"
  },
  "amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      }
    ],
    "label": "月数"
  },
  "user_phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "租客联系电话"
  },
  "user_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "租客姓名"
  },
  "price": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "总价"
  },
  "deposit": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      }
    ],
    "label": "押金"
  },
  "rent_start": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "label": "租房日期"
  },
  "rent_end": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "label": "结束日期"
  },
  "next_charge_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "label": "下次缴费日期"
  },
  "price_type": {
    "rules": [
      {
        "format": "timestamp"
      },
      {
        "range": [
          {
            "text": "月付",
            "value": 1
          },
          {
            "text": "季付",
            "value": 2
          },
          {
            "text": "半年付",
            "value": 3
          },
          {
            "text": "年付",
            "value": 4
          }
        ]
      }
    ],
    "defaultValue": 1,
    "label": "交租类型"
  },
  "contract": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "label": "合同"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "备注"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "进行中",
            "value": 0
          },
          {
            "text": "已结束",
            "value": 1
          }
        ]
      }
    ],
    "defaultValue": 0,
    "label": "订单状态"
  },
  "user_idcard": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "租客身份证号"
  },
  "idcard_url": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "label": "身份证照片"
  },
  "price_per_month": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "月租金"
  },
  "net_fee": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "网费"
  },
  "water_fee": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "水费"
  },
  "fire_fee": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "燃气费"
  }
}

const enumConverter = {
  "price_type_valuetotext": [
    {
      "text": "月付",
      "value": 1
    },
    {
      "text": "季付",
      "value": 2
    },
    {
      "text": "半年付",
      "value": 3
    },
    {
      "text": "年付",
      "value": 4
    }
  ],
  "status_valuetotext": {
    "0": "进行中",
    "1": "已结束"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
